// index.js
import api from "../../api/index";
Page({
  data: {
    numShow: false,
    info: [
      {
        title: "本月(元)",
        price: "0.00",
        sTitle: "会员卡",
        sPrice: "0.00",
        ranking: 0,
        diff: 0,
      },
      {
        title: "今日（元）",
        price: "0.00",
        sTitle: "课程",
        sPrice: "0.00",
        ranking: 0,
        diff: 0,
      },
    ],
    menu: [],
    explainData: {
      title: "统计说明",
      content:
        "本月业绩统计及本月排名同比上月排名涨跌\n今日业绩统计及今日排名同比昨日排名涨跌\n今日会员卡业绩统计，今日课程业绩统计\n向上红色箭头表示同比上升，向下绿箭头表示同比下降",
    },
    explainShow: false,

    shopID: null,
    shopData: {
      name: "shop_id",
      list: [],
    },

    show: false,
    identitys: [],
    appId: "",
  },
  async onShow() {
    let shopID = this.data.shopID;
    if (shopID && shopID !== wx.getStorageSync("shopID")) {
      wx.showLoading({
        title: "加载中",
        mask: true,
      });
      this.getData();
      wx.hideLoading();
    }
  },
  async onLoad(options) {
    this.getIdentitys();
    wx.showLoading({
      title: "加载中",
      mask: true,
    });
    await Promise.all([this.getData(), this.getMenu(), this.getShopList()]);
    wx.hideLoading();
  },

  goUrl(e) {
    let index = e.currentTarget.dataset.index;
    let menu = this.data.menu;
    if (!menu[index].url) {
      wx.showToast({
        title: "正在开发中...",
        icon: "none",
      });
    }
    wx.navigateTo({
      url: menu[index].url,
    });
  },
  getShopValue(e) {
    this.setData({
      shopID: e.detail.value,
    });
    this.getData({
      [e.detail.type]: e.detail.value,
    });
    this.getMenu({
      [e.detail.type]: e.detail.value,
    })
  },
  switchNum(e) {
    this.setData({
      numShow: e.currentTarget.dataset.type == 0,
    });
  },
  getMenu(param = {}) {
    return new Promise((resolve, reject) => {
      api.apiStoreMenu(param).then((res) => {
        this.setData({
          menu: res.data.menu_list
        })
        resolve()
      });
    });
  },
  getData(param = {}) {
    return new Promise((resolve, reject) => {
      api.apiStoreMain(param).then((res) => {
        let { info } = this.data;
        let data = res.data;
        info[0].price = Number(data.earnings_moth).toFixed(2);
        info[0].sPrice = Number(data.earnings_card).toFixed(2);
        info[0].ranking = data.earnings_moth_rank;
        info[0].diff =
          data.earnings_month_change_type === "down"
            ? -data.earnings_moth_diff_value
            : data.earnings_moth_diff_value;

        info[1].price = Number(data.earnings_today).toFixed(2);
        info[1].sPrice = data.earnings_course;
        info[1].ranking = data.earnings_today_rank;
        info[1].diff =
          data.earnings_today_change_type === "down"
            ? -data.earnings_today_diff_value
            : data.earnings_today_diff_value;
        wx.setStorageSync("shopID", data.shop_info.shop_id);
        wx.setStorageSync("userID", data.user_info.user_id);
        this.setData({
          info,
          shopID: data.shop_info.shop_id,
        });
        resolve();
      });
    });
  },
  getShopList() {
    return new Promise((resolve, reject) => {
      api.apiStoreShopList().then((res) => {
        this.setData({
          "shopData.list": res.data.list,
        });
        resolve();
      });
    });
  },

  switchOpenShow(e) {
    this.setData({
      show: +e.currentTarget.dataset.type === 1 ? false : true,
    });
  },
  getIdentitys() {
    api
      .apiAssistIdentity({
        identity: "store",
      })
      .then((res) => {
        this.setData({
          identitys: res.data,
        });
      });
  },
  changeVal(e) {
    console.log(e);
    this.setData({
      appId: e.currentTarget.dataset.id,
    });
  },
  confirm() {
    wx.navigateToMiniProgram({
      appId: this.data.appId, // 必填，字符串类型
      path: "pages/index/index", // 可选，要打开的页面路径及其参数
      // extraData: { // 可选，需要传递给目标小程序的数据，目标小程序可在 App.onLaunch、App.onShow 中获取
      //   foo: 'bar',
      //   key: 'value'
      // },
      envVersion: "release", // 可选。要打开的小程序版本。有效值: 'develop'（开发版），'trial'（体验版），'release'（正式版）。默认是正式版。
      success(res) {
        // 调用成功的回调
        console.log("跳转成功！", res);
      },
      fail(err) {
        // 调用失败的回调
        console.error("跳转失败：", err);
      },
    });
    this.setData({
      show: false,
    });
  },

  explainClose() {
    this.setData({
      explainShow: false,
    });
  },
  explainOpen() {
    this.setData({
      explainShow: true,
    });
  },
});
